package com.gxa.modules.service.impl;

import com.gxa.common.vo.JsonResult;
import com.gxa.common.vo.R;
import com.gxa.modules.mapper.CysMapper;
import com.gxa.modules.mapper.IndiMapper;
import com.gxa.modules.pojo.Carrier_Driver;
import com.gxa.modules.pojo.Indi_Driver;
import com.gxa.modules.service.IndiService;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service
public class IndiServiceImpl implements IndiService {
    @Autowired
    private IndiMapper indiMapper;
    @Autowired
    private CysMapper cysMapper;

    @Override
    public JsonResult findIndiDriver(HashMap map) {
        JsonResult jsonResult = new JsonResult();
        int currentPage = Integer.parseInt(map.get("currentPage").toString());
        int pageSize = Integer.parseInt(map.get("pageSize").toString());
        currentPage = (currentPage-1)*pageSize;
        map.put("currentPage",currentPage);
        map.put("pageSize",pageSize);
        try {
            //查询总数
            Integer count = indiMapper.findzs(map);
            //查询车辆
            List<Carrier_Driver> list = indiMapper.findIndiDriver(map);
            jsonResult.setCode(200);
            jsonResult.setMsg("查询成功");
            jsonResult.setCount(count);
            jsonResult.setData(list);
        }catch (Exception e){
            e.printStackTrace();
            jsonResult.setCode(500);
            jsonResult.setMsg("系统开小差");
        }
        return jsonResult;
    }

    @Override
    public R addIndiDriver(HashMap map) {
        R r = new R();
        try {
            Integer result = indiMapper.addIndiDriver(map);
            if (result > 0){
                //以后可以将盐值生成一个随机数
                String salt = "gxa";
                //盐值
                SimpleHash simpleHash = new SimpleHash("md5", map.get("password").toString(), salt, 3);
                //密码加密+加盐+散列==新的密码
                map.put("password", simpleHash.toString());
                //将盐值
                map.put("salt", salt);
                //调用Mapper实现添加系统用户
                int res = cysMapper.addCysUser(map);
                Integer id = cysMapper.findCysByName(map);
                cysMapper.addCysRole(id);
                r.setCode(200);
                r.setMsg("添加成功");
            }
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差");
        }
        return r;
    }

    @Override
    public R findIndiDriverById(Integer indi_id) {
        R r = new R();
        try {
            Indi_Driver indi_driver = indiMapper.findIndiDriverById(indi_id);
            r.setCode(200);
            r.setMsg("查询成功");
            r.setData(indi_driver);
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差");
        }
        return r;
    }

    @Override
    public R updateIndiDriver(HashMap map) {
        R r = new R();
        try {
            Integer result = indiMapper.updateIndiDriver(map);
            if (result > 0){
                r.setCode(200);
                r.setMsg("编辑成功");
            }
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差");
        }
        return r;
    }

    @Override
    public R delIndiDriver(Integer indi_id) {
        R r = new R();
        try{
            Integer result = indiMapper.delIndiDriver(indi_id);
            if (result>0){
                r.setCode(200);
                r.setMsg("删除成功");
            }
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差");
        }

        return r;
    }
}
